package com.paneedah.weaponlib.render;

import com.paneedah.weaponlib.render.bgl.GLCompatible;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.renderer.texture.TextureUtil;
import net.minecraft.client.shader.Framebuffer;

import java.nio.IntBuffer;

public class HDRFramebuffer extends Framebuffer {

	public HDRFramebuffer(int width, int height, boolean useDepthIn) {
		super(width, height, useDepthIn);
	}
	
	@Override
	public void createFramebuffer(int width, int height) {
		 this.framebufferWidth = width;
	        this.framebufferHeight = height;
	        this.framebufferTextureWidth = width;
	        this.framebufferTextureHeight = height;

	        if (!OpenGlHelper.isFramebufferEnabled())
	        {
	            this.framebufferClear();
	        }
	        else
	        {
	            this.framebufferObject = OpenGlHelper.glGenFramebuffers();
	            this.framebufferTexture = TextureUtil.glGenTextures();

	            if (this.useDepth)
	            {
	                this.depthBuffer = OpenGlHelper.glGenRenderbuffers();
	            }

	            this.setFramebufferFilter(9728);
	            GlStateManager.bindTexture(this.framebufferTexture);
	            GlStateManager.glTexImage2D(3553, 0, GLCompatible.GL_RGBA16F, this.framebufferTextureWidth, this.framebufferTextureHeight, 0, 6408, 5121, (IntBuffer)null);
	            OpenGlHelper.glBindFramebuffer(OpenGlHelper.GL_FRAMEBUFFER, this.framebufferObject);
	            OpenGlHelper.glFramebufferTexture2D(OpenGlHelper.GL_FRAMEBUFFER, OpenGlHelper.GL_COLOR_ATTACHMENT0, 3553, this.framebufferTexture, 0);

	            if (this.useDepth)
	            {
	                OpenGlHelper.glBindRenderbuffer(OpenGlHelper.GL_RENDERBUFFER, this.depthBuffer);
	                if (!isStencilEnabled())
	                {
	                OpenGlHelper.glRenderbufferStorage(OpenGlHelper.GL_RENDERBUFFER, 33190, this.framebufferTextureWidth, this.framebufferTextureHeight);
	                OpenGlHelper.glFramebufferRenderbuffer(OpenGlHelper.GL_FRAMEBUFFER, OpenGlHelper.GL_DEPTH_ATTACHMENT, OpenGlHelper.GL_RENDERBUFFER, this.depthBuffer);
	                }
	                else
	                {
	                    OpenGlHelper.glRenderbufferStorage(OpenGlHelper.GL_RENDERBUFFER, org.lwjgl.opengl.EXTPackedDepthStencil.GL_DEPTH24_STENCIL8_EXT, this.framebufferTextureWidth, this.framebufferTextureHeight);
	                    OpenGlHelper.glFramebufferRenderbuffer(OpenGlHelper.GL_FRAMEBUFFER, org.lwjgl.opengl.EXTFramebufferObject.GL_DEPTH_ATTACHMENT_EXT, OpenGlHelper.GL_RENDERBUFFER, this.depthBuffer);
	                    OpenGlHelper.glFramebufferRenderbuffer(OpenGlHelper.GL_FRAMEBUFFER, org.lwjgl.opengl.EXTFramebufferObject.GL_STENCIL_ATTACHMENT_EXT, OpenGlHelper.GL_RENDERBUFFER, this.depthBuffer);
	                }
	            }

	            this.framebufferClear();
	            this.unbindFramebufferTexture();
	        }
	}

}
